package io.xccit.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.xccit.common.exception.AuthCustomException;
import io.xccit.model.system.SysMenu;
import io.xccit.model.system.SysRoleMenu;
import io.xccit.model.vo.RouterVo;
import io.xccit.system.mapper.ISysMenuMapper;
import io.xccit.system.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.xccit.system.service.ISysRoleMenuService;
import io.xccit.system.util.MenuItemHelper;
import io.xccit.system.util.RouterHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author xccit
 * @since 2023-06-26
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<ISysMenuMapper, SysMenu> implements ISysMenuService {

    @Resource
    private ISysMenuMapper sysMenuMapper;
    @Resource
    private ISysRoleMenuService sysRoleMenuService;

    @Override
    public List<SysMenu> findNodes() {
        List<SysMenu> result = new ArrayList<>();
        //所有菜单
        List<SysMenu> sysMenuList = new ArrayList<>();
        //所有菜单为空,加入一个假数据进行返回
        if (CollectionUtils.isEmpty(sysMenuMapper.selectList(null))) {
            SysMenu sysMenu = new SysMenu();
            sysMenu.setName("暂无菜单");
            result.add(sysMenu);
//            result.forEach(System.out::println);
            return result;
        }
        sysMenuList = sysMenuMapper.selectList(null);
        //若全部菜单有数据,构建树形结构进行返回
        result = MenuItemHelper.buildTree(sysMenuList);
//        result.forEach(System.out::println);
        return result;
    }

    @Override
    public boolean removeMenuById(Long id) {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getParentId,id);
        //把当前ID当作parentID进行查询,返回子菜单数量
        Integer count = sysMenuMapper.selectCount(wrapper);
        if (count > 0){
            throw new AuthCustomException(201,"当前菜单下存在子菜单,不可删除");
        }else{
            sysMenuMapper.deleteById(id);
            return true;
        }
    }

    @Override
    public List<SysMenu> selectParent() {
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getType,0).or().eq(SysMenu::getType,1);
        return sysMenuMapper.selectList(wrapper);
    }

    @Override
    public List<SysMenu> selectMenuByRoleID(Long roleID) {
        //获取所有菜单 附加条件:status == 1
        LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysMenu::getStatus,1);
        List<SysMenu> sysMenuList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sysMenuMapper.selectList(wrapper))){
            sysMenuList = sysMenuMapper.selectList(wrapper);
        }else{
            SysMenu sysMenu = new SysMenu();
            sysMenu.setName("暂无菜单");
            sysMenuList.add(sysMenu);
            return sysMenuList;
        }
        //根据角色ID查询角色分配过的菜单列表
        List<SysRoleMenu> sysRoleMenus = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sysRoleMenuService.selectMenuByRoleID(roleID))){
            sysRoleMenus = sysRoleMenuService.selectMenuByRoleID(roleID);
        }
        //获取角色分配过的菜单ID
        List<Long> hadMenuIDs = sysRoleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        for (SysMenu sysMenu : sysMenuList) {
            if (hadMenuIDs.contains(sysMenu.getId())){
                sysMenu.setSelect(true);
            }
        }
        //转换成树形结构返回
        return MenuItemHelper.buildTree(sysMenuList);
    }

    /**
     * 获取当前用户的菜单集合
     * @param userID
     * @return
     */
    @Override
    public List<RouterVo> getUserMenuList(Long userID) {
        List<SysMenu> sysMenuList = null;
        //默认ID为1的用户是超管,拥有所有权限
        if (userID == 1L){
            LambdaQueryWrapper<SysMenu> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysMenu::getStatus,1);
            sysMenuList = sysMenuMapper.selectList(wrapper);
        }else{
            sysMenuList = sysMenuMapper.selectMenuByUserID(userID);
        }
        //构建树形结构
        List<SysMenu> treeMenuList = MenuItemHelper.buildTree(sysMenuList);
        //转换成前端路由需要的格式返回
        return RouterHelper.buildRouters(treeMenuList);
    }

    /**
     * 获取当前用户的按钮权限集合
     * @param userID
     * @return
     */
    @Override
    public List<String> getUserButtonList(Long userID) {
        List<SysMenu> sysMenuList = null;
        if (userID == 1L){
            sysMenuList = sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getStatus, 1));
        }else{
            sysMenuList = sysMenuMapper.selectMenuByUserID(userID);
        }
        List<String> permsList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            //类型2 表示按钮
            if (sysMenu.getType() == 2){
                String perms = sysMenu.getPerms();
                permsList.add(perms);
            }
        }
        return permsList;
    }

}
